package meijuli.com.join51.business.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.youngfeng.architecture.image.RoundedCornersTransformation;

import java.util.List;

import meijuli.com.join51.business.Confirm;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.PictureDetailActivity;

/**
 * 确认页面
 *
 * @author wenqin 2017-04-14 17:02
 */

public class ConfirmAdapter extends RecyclerView.Adapter<ConfirmAdapter.VH> {

    private LayoutInflater mInflater;
    private List<Confirm> data;
    private Context mContext;

    public ConfirmAdapter(Context context, List<Confirm> data) {
        this.data = data;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(mInflater.inflate(R.layout.adapter_confirm, parent, false));
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        String content = data.get(position).getContent();
        holder.title.setText(data.get(position).getTitle());
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView
                .getLayoutParams();

        if (data.get(position).isContent()) {
            holder.content.setVisibility(View.VISIBLE);
            holder.leftIcon.setVisibility(View.GONE);
            holder.rightIcon.setVisibility(View.GONE);

            holder.content.setText(content == null ? "" : content);
            layoutParams.setMargins(0,0,0,0);
        } else {
            holder.content.setVisibility(View.GONE);
            holder.leftIcon.setVisibility(View.VISIBLE);
            holder.rightIcon.setVisibility(View.VISIBLE);

            if (TextUtils.isEmpty(content)) {
                return;
            }
            String[] paths = content.split(",");

            if (paths.length >= 1) {
                Glide.with(mContext).load(paths[0]).error(R.mipmap.img_default)
                        .into(holder.leftIcon);
                holder.leftIcon.setOnClickListener(new Listener(mContext, paths[0]));
            }

            if (paths.length == 2) {
                Glide.with(mContext).load(paths[1]).error(R.mipmap.img_default)
                        .into(holder.rightIcon);
                holder.rightIcon.setOnClickListener(new Listener(mContext, paths[1]));
            }

            layoutParams.setMargins(0,0,0,32);
        }

        holder.itemView.setLayoutParams(layoutParams);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class VH extends RecyclerView.ViewHolder {

        private TextView title;
        private TextView content;
        private ImageView leftIcon;
        private ImageView rightIcon;

        public VH(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            content = (TextView) itemView.findViewById(R.id.content);
            leftIcon = (ImageView) itemView.findViewById(R.id.left_icon);
            rightIcon = (ImageView) itemView.findViewById(R.id.right_icon);
        }
    }

    private class Listener implements View.OnClickListener {

        private String picPath;
        private Activity mContext;

        public Listener(Context context, String picPath) {
            mContext = (Activity) context;
            this.picPath = picPath;
        }

        @Override
        public void onClick(View v) {
            PictureDetailActivity.start(mContext, picPath);
        }
    }
}
